import { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify';
import { inject } from 'inversify';
import { provide } from 'inversify-binding-decorators';
import UserService from '../services/user.service';

@provide(UserController)
export default class UserController {
  constructor(@inject(UserService) private userService: UserService) { }

  async createUser(request: FastifyRequest, reply: FastifyReply) {
    const { name, email } = request.body as { name: string; email: string };
    const user = await this.userService.createUser(name, email);
    reply.status(201).send(user);
  }

  async getUser(request: FastifyRequest<{ Params: { id: string } }>, reply: FastifyReply) {
    const user = await this.userService.getUserById(parseInt(request.params.id, 10));
    if (!user) {
      reply.status(404).send({ error: 'User not found' });
      return;
    }
    reply.send(user);
  }

  async getAllUsers(request: FastifyRequest, reply: FastifyReply) {
    const users = await this.userService.getAllUsers();
    reply.send(users);
  }
}